Imports System.Collections.Generic
Imports System.Windows.Forms
Imports Vintasoft.Pdf

Namespace Pdf
	Public Partial Class PdfDocumentMessagesForm
		Inherits Form

		#Region "Fields"

		Private _messages As List(Of PdfRuntimeMessage)

		#End Region



		#Region "Constructor"

		Public Sub New(messages As List(Of PdfRuntimeMessage))
			InitializeComponent()

			_messages = messages

			Dim counts As New Dictionary(Of String, Integer)()
			Dim types As New Dictionary(Of String, String)()
			For i As Integer = 0 To messages.Count - 1
				Dim msg As PdfRuntimeMessage = messages(i)
				Dim name As String = msg.ToString()
				If counts.ContainsKey(name) Then
					counts(name) = counts(name) + 1
				Else
					counts.Add(name, 1)
					Dim messageType As String = Nothing
					If TypeOf msg Is PdfRuntimeError Then
						messageType = "Error"
					ElseIf TypeOf msg Is PdfRuntimeWarning Then
						messageType = "Warning"
					Else
						messageType = "Message"
					End If
					types.Add(name, messageType)
				End If
			Next
			FillData(counts, types)
		End Sub

		#End Region



		#Region "Methods"

		Private Sub FillData(counts As Dictionary(Of String, Integer), types As Dictionary(Of String, String))
			For Each name As String In counts.Keys
				Dim index As Integer = dataGridView.Rows.Count
				dataGridView.Rows.Add()
				dataGridView.Rows(index).Cells(0).Value = types(name)
				dataGridView.Rows(index).Cells(1).Value = counts(name)
				dataGridView.Rows(index).Cells(2).Value = name
			Next
		End Sub

		Private Sub dataGridView_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs)
			If e.RowIndex >= 0 AndAlso e.RowIndex < _messages.Count Then
				Dim message As PdfRuntimeMessage = _messages(e.RowIndex)
				Dim text As String = message.ToString()
				If message.InnerException IsNot Nothing Then
					text += Environment.NewLine
					text += String.Format("{0}: {1}", message.InnerException.[GetType](), message.Message)
					text += Environment.NewLine
					text += String.Format("StackTrace: {0}", message.InnerException.StackTrace)
				End If
				Clipboard.SetText(text)
				MessageBox.Show(text)
			End If
		End Sub

		#End Region

	End Class
End Namespace
